
import runSaga from './runSaga'
// import createChannel from './createChannel'
import EventEmitter from 'events';
// let eventEmitter = new EventEmitter();

export default function createSagaMiddleware(){
    // let channel = createChannel()
    let channel = new EventEmitter();

    let boundRunSaga;
    function sagaMiddleware({getState,dispatch}){
        boundRunSaga=runSaga.bind(null,{channel,dispatch,getState});
        return function (next){
            // 这个方法就是改造后的store.dispatch
            return function (action){
                const result = next(action);
                // channel.emit(action)
                channel.emit(action.type,action)
                return result
            }
        }
    }
    sagaMiddleware.run = (saga)=>boundRunSaga(saga)
    return sagaMiddleware
 
} 